﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class ControlPanel_Member_ImageUpload : System.Web.UI.Page
{
    /// <summary>
    /// 图片上传说明:这里图片上传以后是保存到Session，然后再保存到数据库里面
    /// 保存到Session性能上可能会有损失，只能是小容量的文件。
    /// 如果需要大文件上传请自行改进。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["Name"] != null)
            {
                Img_Images.ImageUrl = "Resources.aspx?ID=" + Request.QueryString["Name"].ToString();
            }
            else
            {
                Response.Write("Name=Null");
                Response.End();
            }

            if (Session["ImageData"] != null)
            {
                //读取Session图片
                byte[] imageContent = (byte[])Session["ImageData"];
                MemoryStream imgdatastream = new MemoryStream(imageContent);
                System.Drawing.Image postImage = System.Drawing.Image.FromStream(imgdatastream);

                //控制图片大小
                if (postImage.Height > 120)
                {
                    Img_Images.Height = 120;
                }
                else
                {
                    Img_Images.Height = postImage.Height;
                }
            }
        }
    }
    protected void btn_UpLoadImage_Click(object sender, EventArgs e)
    {
        Boolean fileOK = false;
        String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
        if (FileUploadImage.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(FileUploadImage.FileName).ToLower();

            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }

        if (fileOK)
        {


            HttpPostedFile upPhoto = FileUploadImage.PostedFile;
            //在这里限制图片大小，kB为单位，以后应该做成系统配置
            int photoSize = 100;
            if (upPhoto.ContentLength > photoSize * 1024)
            {
                Response.Write("<script>alert(\"图片大小不能大于" + photoSize.ToString() + "kb,请重新选择！！\");</script>");
            }
            else
            {

                Stream imgdatastream = upPhoto.InputStream;
                string FileName = FileUploadImage.PostedFile.FileName;
                int imgdatalen = FileUploadImage.PostedFile.ContentLength;
                string imgtype = FileUploadImage.PostedFile.ContentType;

                byte[] imgdata = new byte[imgdatalen];

                //读取数据
                int n = imgdatastream.Read(imgdata, 0, imgdatalen);
                System.Drawing.Image postImage = System.Drawing.Image.FromStream(imgdatastream);
                Session["ImageData"] = imgdata;
                Session["ImageType"] = imgtype;
                Img_Images.ImageUrl = "Resources.aspx?Id=" + FileName;


                if (postImage.Height > 120)
                {
                    Img_Images.Height = 120;
                }
                else
                {
                    Img_Images.Height = postImage.Height;
                }
            }
        }
        else
        {
            Response.Write("<script>alert(\"文件格式只支持" + allowedExtensions.ToString() + "\");</script>");

            Response.Write("");
        }
    }



}
